git bisect 사용하여 버그 시작지점 검색하기

ChatGPT 4o answer

git bisect is a binary search tool built into Git that helps you quickly identify the commit that introduced a bug or regression. Instead of manually checking each commit, Git automates the search by checking commits halfway between a known good and bad commit.


🔧 Basic Workflow

  1. Start bisect:

    git bisect start
    
  2. Mark the current (bad) commit:

    git bisect bad
    
  3. Mark a known good commit (where the bug didn't exist):

    git bisect good <good_commit_hash>
    
  4. Git will now checkout a commit halfway between good and bad. You test it manually:

    • If the bug is present:

      git bisect bad
      
    • If the bug is not present:

      git bisect good
      
  5. Repeat step 4 until Git finds the exact commit that introduced the issue.

  6. End bisecting session:

    git bisect reset
    

🧪 Example

git bisect start
git bisect bad                # current commit has a bug
git bisect good abc1234       # abc1234 is a known good commit
# Test commit checked out by Git
git bisect bad                # if buggy
# Repeat until Git outputs:
# <bad_commit_hash> is the first bad commit
git bisect reset              # go back to original HEAD

🛠️ Bonus: Automate Testing

If you have a test script (exit code 0 = good, non-zero = bad):

git bisect run ./test-script.sh

🎯 Use Cases

Would you like an example use case applied to a real-world project?